package com.zy.web.servlet;

import com.zy.pojo.Employee;
import com.zy.pojo.ResultInfo;
import com.zy.pojo.Role;
import com.zy.service.EmployeeService;
import com.zy.service.impl.EmployeeServiceImpl;
import com.zy.web.servlet.base.BaseServlet;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: xiaoqi
 * Date: 2021/5/31
 * Time: 下午 15:48
 * Description: 职工servlet
 */
@WebServlet("/employee/*")
public class EmployeeServlet extends BaseServlet {

    private EmployeeService employeeService = new EmployeeServiceImpl();

    public void getAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Integer page = Integer.parseInt(request.getParameter("page"));
        Integer limit = Integer.parseInt(request.getParameter("limit"));
        String name = request.getParameter("name");
        String phone = request.getParameter("phone");
        String email = request.getParameter("email");
        String department = request.getParameter("department");
        Employee employee = new Employee();
        employee.setName(name);
        employee.setPhone(phone);
        employee.setEmail(email);
        Integer departmentId = null;
        try {
            departmentId = Integer.parseInt(department);
        } catch (NumberFormatException e) {
        }
        employee.setDepartmentId(departmentId);
        List<Employee> employeeList = employeeService.getEmployeeListPage(page, limit, employee);
        Integer count = employeeService.getEmployeeCount(employee);
        HashMap<String, Object> result = new HashMap<>();
        result.put("code", 0);
        result.put("message", "操作成功");
        result.put("count", count);
        result.put("data", employeeList);
        writeValue(result, response);
    }

    public void getEmpById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    }

    public void save(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Integer id = null;
        try {
            id = Integer.parseInt(request.getParameter("id"));
        } catch (NumberFormatException e) {
            id = null;
            System.out.println("添加用户");
        }
        String name = request.getParameter("name");
        String password = request.getParameter("password");
        String phone = request.getParameter("phone");
        String roleName = request.getParameter("roleName");
        String email = request.getParameter("email");
        String enterDateStr = request.getParameter("enterDate");
        Integer departmentId = Integer.parseInt(request.getParameter("departmentId"));
        String img = request.getParameter("img");
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date enterDate = null;
        try {
            enterDate = format.parse(enterDateStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Employee employee = new Employee(id, name, password, phone, email, enterDate, departmentId, img);
        Role role = new Role();
        role.setEmpId(id);
        role.setRoleName(roleName);
        employee.setRole(role);
        boolean result = false;
        if (employee.getId() != null && employee.getId() != 0) {
            result = employeeService.updateEmployee(employee);
        } else {
            result = employeeService.insertEmployee(employee);
        }

        ResultInfo resultInfo = new ResultInfo();
        if (result) {
            resultInfo.setCode(200);
            resultInfo.setMessage("操作成功");
        } else {
            resultInfo.setCode(400);
            resultInfo.setMessage("操作失败");
        }
        writeValue(resultInfo, response);
    }

    public void delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Integer id = Integer.parseInt(request.getParameter("id"));
        Boolean result = employeeService.deleteEmployeeById(id);
        ResultInfo resultInfo = new ResultInfo();
        if (result) {
            resultInfo.setCode(200);
            resultInfo.setMessage("删除成功");
        } else {
            resultInfo.setCode(400);
            resultInfo.setMessage("删除失败");
        }
        writeValue(resultInfo, response);
    }

    public void deleteList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String ids = request.getParameter("ids");
        String[] split = ids.split(",");
        for (String temp : split) {
            employeeService.deleteEmployeeById(Integer.parseInt(temp));
        }
        ResultInfo resultInfo = new ResultInfo();
        resultInfo.setCode(200);
        resultInfo.setMessage("删除成功");
        writeValue(resultInfo, response);
    }

    public void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    }
}